<template>
  <div class="admin-layout">
    <!-- 顶部导航栏 -->
    <div class="admin-header">
      <div class="header-left">
        <router-link to="/" class="back-to-home">
          <el-icon><Back /></el-icon>
          <span>返回商城</span>
        </router-link>
        <div class="divider"></div>
        <h2>卖家后台管理系统</h2>
      </div>
      <div class="header-right">
        <el-dropdown>
          <span class="user-info">
            <el-avatar size="small" />
            <span>管理员</span>
          </span>
          <template #dropdown>
            <el-dropdown-menu>
              <el-dropdown-item>个人信息</el-dropdown-item>
              <el-dropdown-item>修改密码</el-dropdown-item>
              <el-dropdown-item divided @click="handleLogout">退出登录</el-dropdown-item>
            </el-dropdown-menu>
          </template>
        </el-dropdown>
      </div>
    </div>

    <!-- 主体内容区 -->
    <div class="admin-main">
      <!-- 左侧菜单 -->
      <div class="admin-sidebar">
        <el-menu
          :default-active="activeMenu"
          class="admin-menu"
          router
          background-color="#001529"
          text-color="#fff"
          active-text-color="#409EFF"
        >
        <el-menu-item index="/admin/statistics">
            <el-icon><TrendCharts /></el-icon>
            <span>数据统计</span>
          </el-menu-item>
          <el-menu-item index="/admin/products">
            <el-icon><Goods /></el-icon>
            <span>商品管理</span>
          </el-menu-item>
          <el-menu-item index="/admin/orders">
            <el-icon><List /></el-icon>
            <span>订单管理</span>
          </el-menu-item>
          <el-menu-item index="/admin/inventory">
            <el-icon><Files /></el-icon>
            <span>库存管理</span>
          </el-menu-item>
          <el-menu-item index="/admin/marketing">
            <el-icon><Promotion /></el-icon>
            <span>营销管理</span>
          </el-menu-item>
          <el-menu-item index="/admin/customers">
            <el-icon><User /></el-icon>
            <span>客户管理</span>
          </el-menu-item>
          <el-menu-item index="/admin/finance">
            <el-icon><Money /></el-icon>
            <span>财务管理</span>
          </el-menu-item>
          <el-menu-item index="/admin/settings">
            <el-icon><Setting /></el-icon>
            <span>店铺设置</span>
          </el-menu-item>
        </el-menu>
      </div>

      <!-- 右侧内容区 -->
      <div class="admin-content">
        <div class="content-header">
          <el-breadcrumb separator="/">
            <el-breadcrumb-item :to="{ path: '/admin' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>{{ currentRoute.meta.title || '管理后台' }}</el-breadcrumb-item>
          </el-breadcrumb>
        </div>
        <div class="content-main">
          <router-view v-slot="{ Component }">
            <keep-alive>
              <component :is="Component" />
            </keep-alive>
          </router-view>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, computed } from 'vue';
import { useRoute, useRouter } from 'vue-router';
import {
  Goods,
  List,
  Files,
  TrendCharts,
  Setting,
  User,
  Money,
  Promotion,
  Back
} from '@element-plus/icons-vue';
import { ElMessage } from 'element-plus';

const route = useRoute();
const router = useRouter();

const activeMenu = computed(() => route.path);
const currentRoute = computed(() => route);

const handleLogout = () => {
  localStorage.removeItem('adminToken');
  router.push('/admin/login');
  ElMessage.success('已退出登录');
};
</script>

<style scoped>
.admin-layout {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

.admin-header {
  height: 64px;
  background-color: #fff;
  padding: 0 24px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  box-shadow: 0 1px 4px rgba(0, 21, 41, 0.08);
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  z-index: 1000;
}

.admin-main {
  display: flex;
  margin-top: 64px;
  min-height: calc(100vh - 64px);
}

.admin-sidebar {
  width: 200px;
  background-color: #001529;
  position: fixed;
  top: 64px;
  left: 0;
  bottom: 0;
  overflow-y: auto;
}

.admin-content {
  margin-left: 200px;
  flex: 1;
  padding: 24px;
  background-color: #f0f2f5;
  min-height: calc(100vh - 64px);
}

.content-header {
  background-color: #fff;
  padding: 16px 24px;
  border-radius: 4px;
  margin-bottom: 24px;
}

.content-main {
  background-color: #fff;
  padding: 24px;
  border-radius: 4px;
  min-height: calc(100vh - 220px);
}

.header-left {
  display: flex;
  align-items: center;
  gap: 16px;
}

.back-to-home {
  display: flex;
  align-items: center;
  gap: 8px;
  color: #666;
  text-decoration: none;
  font-size: 14px;
  transition: all 0.3s;
}

.back-to-home:hover {
  color: #409EFF;
}

.divider {
  width: 1px;
  height: 24px;
  background-color: #e8e8e8;
}

.header-left h2 {
  margin: 0;
  font-size: 18px;
  color: #001529;
}

.admin-menu {
  border-right: none;
}

:deep(.el-menu-item) {
  height: 50px;
  line-height: 50px;
}

:deep(.el-menu-item .el-icon) {
  margin-right: 10px;
  font-size: 18px;
}

:deep(.el-menu-item.is-active) {
  background-color: #1890ff !important;
}

.user-info {
  display: flex;
  align-items: center;
  gap: 8px;
  cursor: pointer;
  color: #666;
}

/* 滚动条样式 */
.admin-sidebar::-webkit-scrollbar {
  width: 6px;
}

.admin-sidebar::-webkit-scrollbar-thumb {
  background-color: rgba(255, 255, 255, 0.2);
  border-radius: 3px;
}

.admin-sidebar::-webkit-scrollbar-track {
  background-color: transparent;
}
</style>